package user.ucm;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import user.view.MainView;

public class UCPaying implements UCController {

    private MainView mv;
    private double money;
    private int num;

    public UCPaying(MainView mv) {
        this.mv = mv;
    }

    public int run() {
        try {
            Class.forName("com.mysql.jdbc.Driver");

            Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/cp3120?user=root&password=root");
            con.setCatalog("cp3120");

            Statement statement = con.createStatement();
            num = Integer.valueOf(mv.getAirVModel().getNum_TextField().getText());
            if (num > 1) {
                money = num * Integer.valueOf(mv.getAirVModel().getPrice_TextField().getText()) * 0.9;
            }else{
                money = Integer.valueOf(mv.getAirVModel().getPrice_TextField().getText());
            }
            String sql = "INSERT INTO pay VALUES(" +
                    "'" + mv.getUserModel().getName() + "','" + mv.getAirVModel().getAirName_TextField().getText() + "',10," +
                    String.valueOf(num) + "," + String.valueOf(money) + ")";
            System.out.println(sql);
            statement.execute(sql);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
